from tqsdk import TqApi, TqAuth, TqKq
from tqsdk.ta import MACD
import asyncio

async def macd_strategy():
    # 创建天勤API实例(使用快期模拟账户)
    api = TqApi(auth=TqAuth("您的账号", "您的密码"), account=TqKq())
    
    # 获取K线数据(这里以螺纹钢主力合约为例)
    klines = api.get_kline_serial("SHFE.rb2405", 900)  # 15分钟K线
    
    # 持仓变量
    position = None
    
    while True:
        # 等待K线更新
        async with api.register_update_notify() as update_notify:
            await update_notify.wait()
        
        # 计算MACD指标
        macd_data = MACD(klines, 12, 26, 9)
        
        # 获取最新MACD值
        dif = macd_data["dif"][-1]
        dea = macd_data["dea"][-1]
        prev_dif = macd_data["dif"][-2]
        prev_dea = macd_data["dea"][-2]
        
        # 金叉条件: DIF上穿DEA
        golden_cross = prev_dif <= prev_dea and dif > dea
        
        # 死叉条件: DIF下穿DEA
        dead_cross = prev_dif >= prev_dea and dif < dea
        
        # 获取当前持仓
        position = api.get_position("SHFE.rb2405")
        
        # 交易逻辑
        if golden_cross and (position is None or position.pos == 0):
            print("出现金叉信号，开多仓")
            api.insert_order(symbol="SHFE.rb2405", direction="BUY", offset="OPEN", volume=1)
            
        elif dead_cross and (position is not None and position.pos > 0):
            print("出现死叉信号，平多仓")
            api.insert_order(symbol="SHFE.rb2405", direction="SELL", offset="CLOSE", volume=1)

if __name__ == "__main__":
    # 运行策略
    asyncio.run(macd_strategy())
